function vectorMultiplyScalar(vector, scalar) {
    let vec = new Float32Array(vector.length);
    for (let k in vec) {
        vec[k] = vector[k] * scalar;
    }
    return vec;
}

function vectorAddVector(vector1, vector2) {
    let vector = new Float32Array(vector1.length);
    for (let i = 0; i < vector.length; i++) {
        vector[i] = vector1[i] + vector2[i];
    }
    return vector;
}

function vectorMultiplyVector(vector1, vector2) {
    let scalar = 0;
    for (let i = 0; i < vector1.length; i++) {
        scalar += vector1[i] * vector2[i];
    }
    return scalar;
}

function vectorCrossVector(vector1, vector2) {
    let vector = new Float32Array(vector1.length);
    let len = vector1.length;
    for (let i = 0; i < vector.length; i++) {
        let aIndex = (i + 1) % len;
        let bIndex = (i + 2) % len;
        vector[i] = vector1[aIndex] * vector2[bIndex] - vector1[bIndex] * vector2[aIndex];
    }
    return vector;
}

function scalarMultiplyMatrix(scalar, matrix) {
    let mat = new Float32Array(matrix.length);
    for (let k in mat) {
        mat[k] = matrix[k] * scalar;
    }
    return mat;
}

function matrixMultiplyMatrix(matrix1, matrix2) {
    let mat = new Float32Array(matrix1.length);
    let len = Math.sqrt(mat.length);

    function getIndex(c, r) {
        return c * len + r;
    }

    for (let i = 0; i < len; i++) {
        for (let j = 0; j < len; j++) {
            for (let k = 0; k < len; k++) {
                mat[getIndex(i, j)] += matrix1[getIndex(i, k)] * matrix2[getIndex(k, j)]
            }
        }
    }
    return mat;
}

function vectorMultiplyMatrix(vector, matrix) {
    let vec = new Float32Array(vector.length);
    let len = vec.length;

    function getIndex(c, r) {
        return c * len + r;
    }

    for (let j = 0; j < len; j++) {
        for (let k = 0; k < len; k++) {
            vec[j] += vector[k] * matrix[getIndex(k, j)];
        }
    }

    return vec;
}

var vector = new Float32Array([1, 2, 3]);

console.log(vectorMultiplyScalar(vector, 2));

var vector1 = new Float32Array([1, 2, 3]);
var vector2 = new Float32Array([2, 3, 4]);
console.log(vectorAddVector(vector1, vector2));

console.log(vectorMultiplyVector(vector1, vector2));

console.log(vectorCrossVector(vector1, vector2));

var mat = new Float32Array([
    1, 2, 3,
    4, 5, 6,
    7, 8, 9,
]);

console.log(scalarMultiplyMatrix(2, mat));

var vec = new Float32Array([1, 2]);

var mat1 = new Float32Array([
    -3, 0,
    5, 0.5,
]);

var mat2 = new Float32Array([
    -7, 2,
    4, 6,
]);

console.log(matrixMultiplyMatrix(mat1, mat2));

console.log(vectorMultiplyMatrix(vec, mat1));

